package com.xinluxinli.caiwu.service;

import com.xinluxinli.caiwu.entity.EmployeeInfo;
import com.xinluxinli.caiwu.entity.EmployeeInfoVo;
import com.xinluxinli.caiwu.mapper.EmployeeInfoMapper;
import javafx.scene.input.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmployeeInfoService {
    @Autowired
    private EmployeeInfoMapper employeeInfoMapper;

    public void addEmployeeInfo(EmployeeInfoVo vo) throws ParseException {
        EmployeeInfo employeeInfo = cover2DbEntity(vo);
        employeeInfoMapper.insert(employeeInfo);
    }

    private EmployeeInfo cover2DbEntity(EmployeeInfoVo vo) throws ParseException {
        EmployeeInfo employeeInfo = new EmployeeInfo();
        employeeInfo.setEmployee(vo.getEmployee());
        employeeInfo.setPack(Integer.parseInt(vo.getPack()));
        employeeInfo.setCustomer(vo.getCustomer());
        employeeInfo.setService_time(Integer.parseInt(vo.getService_time()));
        employeeInfo.setAmount(Integer.parseInt(vo.getAmount()));
        int first = Integer.parseInt(vo.getIs_first());
        employeeInfo.setIs_first(first);
        int times = first == 0 ? Integer.parseInt(vo.getService_times()) : 0;
        employeeInfo.setService_times(times);
        employeeInfo.setPay_type(Integer.parseInt(vo.getPay_type()));
        String commission = vo.getCommission();

        employeeInfo.setCommission(Float.parseFloat(commission.substring(0, commission.length() - 1)) / 100);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(vo.getCtime());
        employeeInfo.setCtime(date);
        return employeeInfo;
    }

    public List<EmployeeInfoVo> queryEmployeeInfo(String employee, String date) {
        List<EmployeeInfo> employeeInfos = employeeInfoMapper.queryByemployeeAndDate(employee, date);
        if (CollectionUtils.isEmpty(employeeInfos)) {
            return new ArrayList<>();
        }
        List<EmployeeInfoVo> employeeInfoVos = employeeInfos.stream().map(e -> {
            EmployeeInfoVo vo = new EmployeeInfoVo();

            vo.setEmployee(e.getEmployee());
            Integer pack = e.getPack();
            switch (pack) {
                case 1:
                    vo.setPack("单次消费");
                    break;
                case 2:
                    vo.setPack("包年");
                    break;
                case 3:
                    vo.setPack("季度");
                    break;
                case 4:
                    vo.setPack("半年");
                    break;
            }
            vo.setCustomer(e.getCustomer());
            vo.setService_time(String.valueOf(e.getService_time()));
            if (e.getIs_first() == 1) {
                vo.setIs_first("首次/"+String.valueOf(e.getService_times()));
            } else {
                vo.setIs_first("非首次/"+String.valueOf(e.getService_times()));
            }
            Integer payType = e.getPay_type();
            switch (payType) {
                case 1:
                    vo.setPay_type("微信");
                    break;
                case 2:
                    vo.setPay_type("支付宝");
                    break;
                case 3:
                    vo.setPay_type("现金");
                    break;
                case 4:
                    vo.setPay_type("刷卡");
                    break;
                case 5:
                    vo.setPay_type("其他");
                    break;
            }
            Float commission = e.getCommission() * 100;
            vo.setCommission(commission.intValue()+ "%");

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            vo.setCtime(sdf.format(e.getCtime()));
            vo.setAmount(String.valueOf(e.getAmount()));
            return vo;

        }).collect(Collectors.toList());

        return employeeInfoVos;
    }
}
